import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author quan
 * @Description 子页面
 * @Date 2023/6/29 19:31
 */
@WebServlet("/index")
public class indexServlet extends HttpServlet {
    //获取当前用户Session
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、设置编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        //2、获取当前用户的session:
        HttpSession session = req.getSession(false);
        //3、判断当前用户是否登录
        if(session == null){
            resp.getWriter().write("没有登录，请重新登录");
            return;
        }
        //4、说明已经登录了，判断用户存不存在
        //获取当前登录的用户对象
        String username = (String) session.getAttribute("userInfo");
        //判断该对象名是否正确
        if(username == null || username.equals("")){
            resp.getWriter().write("没有找到用户信息，请重新登录");
            return;
        }
        //否则:登录成功，页面上显示用户名
        resp.getWriter().write("欢迎回来"+username);
    }
}
